package com.hoyo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.common.PageInfo;
import com.hoyo.domain.Catalog;
import com.hoyo.domain.Channel;
import com.hoyo.domain.Comment;
import com.hoyo.domain.CountInfo;
import com.hoyo.domain.IndexPageChannel;
import com.hoyo.domain.Movie;
import com.hoyo.mapper.CatalogMapper;
import com.hoyo.mapper.ChannelMapper;
import com.hoyo.mapper.CommentMapper;
import com.hoyo.mapper.MovieMapper;
import com.hoyo.utils.TokenUtils;

@Service
public class RouteServiceImpl implements RouteService{
	
	@Autowired
	private ChannelMapper channelMapper;
	
	@Autowired
	private CatalogMapper catalogMapper;
	
	@Autowired
	private MovieMapper movieMapper;
	
	@Autowired
	private CommentMapper commentMapper;

	@Override
	public List<IndexPageChannel> indexPrepared() throws Exception {
		ArrayList<IndexPageChannel> channels = new ArrayList<>();
		List<Channel> tmpChannels = channelMapper.findAllChannelsAndCountMovieInEveryChannel();
		for (Channel channel : tmpChannels) {
			//get 10 catalog of the latest publish catalogs with channel id 1
			List<Catalog> top10Catalogs = catalogMapper.findCatalogsByChannelIdOrderByDateDescLimitBy(channel.getId(), 10);
			//get 7  catalog of the play top catalogs with channel id 1
			List<Catalog> top7Catalogs = catalogMapper.findCatalogsOrderByPlayAmountDescLimitBy(channel.getId(), 7);
			IndexPageChannel tmphannel = new IndexPageChannel(channel.getId(), channel.getChannelName(), channel.getMovieCount(), top10Catalogs, top7Catalogs);
			channels.add(tmphannel);
		}
		
		return channels;
	}

	@Override
	public Catalog catalogPrepared(int catalogId) throws Exception {
		Catalog catalog = catalogMapper.findById(catalogId);
		List<Movie> movies = movieMapper.findMoviesByCatalogId(catalogId);
//		enable the first movie to be play, means we should translate the movie path 2 the real path
		Movie firstMovie = movies.get(0);
		firstMovie.setRealPath(TokenUtils.generateDownloadToken(firstMovie.getRealPath()));
		catalog.setMovies(movies);
//		update the movie play amount of the first movie
		movieMapper.updatePlayAmountByMovieIdAndPlusAmount(firstMovie.getId(), 1);
//		setup comments of the first movie
		List<Comment> comments = commentMapper.findCommentsByMovieIdOrderByCreateTimeDescAndLimitBy(firstMovie.getId(), 0, 10);
		for (Comment comment : comments) {
			List<Comment> replyComments = commentMapper.findReplyCommentsByCommentIdOrderByLikPointsDescAndLimitBy(comment.getId(), 0, 3);
			comment.setPageInfo(new PageInfo<>(1, 3, replyComments.size()>0?replyComments.get(0).getTotal():0, replyComments));
		}
		firstMovie.setComments(new PageInfo<>(1, 10, comments.size()>0?comments.get(0).getTotal():0, comments));
		System.out.println(JSONObject.toJSONString(catalog));
		return catalog;
	}

	@Override
	public JSONObject adminPrepared() throws Exception {
		JSONObject obj = new JSONObject();
		CountInfo countInfo = movieMapper.getCountInfo();
		List<Catalog> catalogs = catalogMapper.findCatalogsByChecked(false, 0, 4);
		obj.put("countInfo", countInfo);
		obj.put("catalogs", catalogs);
		return obj;
	}
}
